package com.self.smallma.myblogweb.service.impl;

import com.self.smallma.myblogweb.dao.BlogDao;
import com.self.smallma.myblogweb.vo.CommnetVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.self.smallma.myblogweb.common.utils.PageUtils;
import com.self.smallma.myblogweb.common.utils.Query;

import com.self.smallma.myblogweb.dao.CommentDao;
import com.self.smallma.myblogweb.entity.CommentEntity;
import com.self.smallma.myblogweb.service.CommentService;


@Service("commentService")
public class CommentServiceImpl extends ServiceImpl<CommentDao, CommentEntity> implements CommentService {

    @Autowired
    BlogDao blogDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<CommentEntity> page = this.page(
                new Query<CommentEntity>().getPage(params),
                new QueryWrapper<CommentEntity>()
        );

        return new PageUtils(page);
    }

    //存放迭代找出的所有子代的集合
    private List<CommnetVo> tempReplys = new ArrayList<>();

    @Override
    public List<CommnetVo> listCommentByBlogId(Long blogId) {
        QueryWrapper<CommentEntity> wrapper = new QueryWrapper<CommentEntity>().eq("blog_id",blogId);
        wrapper.and(w->{
            w.eq("parent_comment_id",Long.parseLong("-1"));
        }).orderByAsc("create_time");
        //查询出父节点
        List<CommentEntity> commentEntities = this.baseMapper.selectList(wrapper);
        List<CommnetVo> collect = commentEntities.stream().map(item -> {
            CommnetVo commnetVo = new CommnetVo();
            BeanUtils.copyProperties(item, commnetVo);
            return commnetVo;
        }).collect(Collectors.toList());
        for (CommnetVo comment :collect){
            Long id = comment.getCommentId();
            String parentNickname1  = comment.getNickname();
            QueryWrapper<CommentEntity> queryWrapper = new QueryWrapper<CommentEntity>().eq("blog_id",blogId);
            queryWrapper.and(w->{
                w.eq("parent_comment_id",id);
            });
            List<CommentEntity> tmp  = this.baseMapper.selectList(queryWrapper);
            List<CommnetVo> childComments = tmp.stream().map( item-> {
                CommnetVo c = new CommnetVo();
                BeanUtils.copyProperties(item, c);
                return c;
            }).collect(Collectors.toList());
            childComments.forEach(System.out::println);
            //            查询出子评论
            combineChildren(blogId,childComments,parentNickname1 );
            comment.setReplyComments(tempReplys);
               tempReplys = new ArrayList<>();
        }
        collect.forEach(System.out::println);
        return collect;


    }

    @Override
    public void saveToVo(CommnetVo comment) {
        if (comment!=null){
            CommentEntity commentEntity = new CommentEntity();
            BeanUtils.copyProperties(comment,commentEntity);
            commentEntity.setCreateTime(new Date());

            //TODO 文章评论计数
            blogDao.getCommentCountById(comment.getBlogId());
            this.save(commentEntity);
        }

    }

    private void combineChildren(Long blogId, List<CommnetVo> childComments, String parentNickname1) {
        //        判断是否有一级子评论
        if (childComments.size()>0){

            for(CommnetVo childComment : childComments){
                String parentNickname = childComment.getNickname();
                childComment.setParentNickname(parentNickname1);
                tempReplys.add(childComment);
                Long childId = childComment.getCommentId();
                //                    查询出子二级评论
                recursively(blogId, childId, parentNickname);
            }

        }
    }

    private void recursively(Long blogId, Long childId, String parentNickname1) {

        //        根据子一级评论的id找到子二级评论
        QueryWrapper<CommentEntity> wrapper = new QueryWrapper<CommentEntity>().eq("blog_id",blogId);
        wrapper.and(w->{
            w.eq("parent_comment_id",childId);
        }).orderByAsc("create_time");
        List<CommentEntity> temp = this.baseMapper.selectList(wrapper);
        List<CommnetVo> collect = temp.stream().map(item -> {
            CommnetVo commnetVo = new CommnetVo();
            BeanUtils.copyProperties(item, commnetVo);
            return commnetVo;
        }).collect(Collectors.toList());
        if (collect.size()>0){
            for(CommnetVo replayComment : collect){
                String parentNickname = replayComment.getNickname();
                replayComment.setParentNickname(parentNickname1);
                Long replayId = replayComment.getCommentId();
                tempReplys.add(replayComment);
                recursively(blogId,replayId,parentNickname);
            }
        }
    }

}